import { queryQuestion } from './service';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import { queryDictList } from '@/services/dict';

export default {
  namespace: 'analyse_danger',

  state: {
    total: {},
    details: [],
    water: {}
  },

  effects: {
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *updateQuestionCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setQuestionCond', payload: cond });
      yield put({ type: 'queryQuestion', payload })
    },
    *queryQuestion({ payload }, { call, put, select }) {
      const cond = yield select(state => state.analyse_danger.questionCond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: {keyList: DICT} });
      const { list = [], total } = yield call(queryQuestion, params);
      yield put({ type: 'setQuestion', payload: { data: list, dataCnt: +total } });
    }
  },

  reducers: {
    setQuestionCond(state, action) {
      return {
        ...state,
        questionCond: action.payload
      }
    },
    setQuestion(state, action) {
      return {
        ...state,
        questionData: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    }
  }
}
